<template>
  <div class="login-box">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <h3 class="login-title">千峰旅业后台管理系统登录</h3>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="form.userName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="form.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit(form)">登录</el-button>
        <el-button type="defailt" class="resetBtn" v-on:click="rnull"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "login",
  data() {
    return {
      form: {
        userName: "admin1",
        password: "123456",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 4, max: 6, message: "长度在 4 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    onSubmit: function (formName) {
      //alert(formName.name+" "+formName.password);
      //this.$router.push("/main");
      //使用axios向后端发出登录请求
      this.$axios
        .post("/admin/login", formName)
        .then((res) => {
          console.log(res);
          localStorage.setItem("token", res.data.info);
          //sessionStorage.setItem("userName", res.data.info.loginName);
          //当请求成功后页面导航到后台主页main
          if (res.data.statusCode == 200) {
            this.$message({
              message: formName.userName + ",登录成功",
              type: "success",
            });
            this.$router.push({ path: "/main", query: formName });
          }else if(res.data.statusCode == -100){
            this.$alert(res.data.msg, '提示', {
              confirmButtonText: '确定'}
            );
          }
           else {
            this.$message.error("登录失败，账号或密码错误");
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message.error("Error: Network Error");
        });
    },
    rnull: function () {
      this.form.userName = "";
      this.form.password = "";
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login-title {
  text-align: center;
  margin: 30px auto;
}
.login-box {
  width: 400px;
  height: 300px;
  border: 1px solid #dcdfe6;
  margin: 50px auto;
  padding-right: 40px;
  border-radius: 8px;
  box-shadow: 0px 8px 16px silver;
}
</style>
